﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CQIE.TES.IDAL;
using CQIE.TES.Model;
using CQIE.TES.WEB.USER.Models;

namespace CQIE.TES.DAL
{
    public class EvaluaHeightDAL:IEvaluaHeightDAL
    {
        [Obsolete("这只是曾经的一个测试方法，不要使用！")]
        public bool AddnewEva(EvaluaHeight model)
        {
            using (MyDB db = new MyDB())
            {
                db.EvaluaHeight.Add(model);

                int count = db.SaveChanges();
                if (count==0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        /// <summary>
        /// 通过角色获取评教
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public List<EvaluaHeight> GetEvaluaHeightsByRole(ROLE_ENUM role)
        {
            using (MyDB db = new MyDB())
            {
                EvaluaHeight d = new EvaluaHeight();
                if (role == ROLE_ENUM.Teacher)
                {
                    return db.EvaluaHeight.Where(r =>
                        r.THeight > 0 && r.StartTime < DateTime.Now && r.EndTime > DateTime.Now).ToList();
                }
                else if (role == ROLE_ENUM.Leader)
                {
                    return db.EvaluaHeight.Where(r =>
                        r.LHeight > 0 && r.StartTime < DateTime.Now && r.EndTime > DateTime.Now).ToList();
                }
                else if (role == ROLE_ENUM.Student)
                {
                    return db.EvaluaHeight.Where(r =>
                        r.SHeight > 0 && r.StartTime < DateTime.Now && r.EndTime > DateTime.Now).ToList();
                }
                else if (role==ROLE_ENUM.Department)
                {
                    return db.EvaluaHeight.Where(r =>
                        r.DHeight > 0 && r.StartTime < DateTime.Now && r.EndTime > DateTime.Now).ToList();
                }
                else if (role==ROLE_ENUM.Self)
                {
                    return db.EvaluaHeight.Where(r =>
                        r.MHeight > 0 && r.StartTime < DateTime.Now && r.EndTime > DateTime.Now).ToList();
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 获取所有评教
        /// </summary>
        /// <returns></returns>
        public List<EvaluaHeight> GetAllEva(int page, int pageSize, out int count, int studyYearId = -1)
        {
            using (MyDB db = new MyDB())
            {
                List<EvaluaHeight> data;
                var query = db.EvaluaHeight.Include(r => r.StudyYear).OrderBy(r => r.ID);
                if (studyYearId.Equals(-1))
                {
                    count = query.Count();
                    data = query.Skip(page * pageSize).Take(pageSize).ToList();
                }
                else
                {
                    count = query.Count(r => r.StudyYearId == studyYearId);
                    data = query.Where(r => r.StudyYearId == studyYearId).Skip(page * pageSize)
                        .Take(pageSize).ToList();
                }

                return data;
            }
        }

        public bool StartEvaNow(int evaId)
        {
            using (MyDB db = new MyDB())
            {
                var model = db.EvaluaHeight.Find(evaId);
                model.StartTime = DateTime.Now;
                if (db.SaveChanges()>0)
                {
                    return true;
                }

                return false;
            }
        }

        public bool EndEvaNow(int evaId)
        {
            using (MyDB db = new MyDB())
            {
                var model = db.EvaluaHeight.Find(evaId);
                model.EndTime = DateTime.Now;
                if (db.SaveChanges() > 0)
                {
                    return true;
                }

                return false;
            }
        }

        
        public bool HaveEvaing()
        {
            using (MyDB db = new MyDB())
            {
                if (db.EvaluaHeight.Any(r => r.StartTime<DateTime.Now && r.EndTime>DateTime.Now))
                {
                    return true;
                }

                return false;
            }
        }
    }
}
